//秒杀倒计时
function countDown(endTimeList){
  var nowtime = Date.parse(new Date()) / 1000;
  let len = endTimeList.length;
  let countDownArr = {};
  for (var i = 0; i < len; i++) {
    var endtime = endTimeList[i];
    var obj = null;
    var second = endtime - nowtime;
    // 天数位
    var day = Math.floor(second / 3600 / 24);
    var dayStr = day.toString();
    if (dayStr.length == 1) dayStr = '0' + dayStr;

    // 小时位
    var hr = Math.floor((second - day * 3600 * 24) / 3600);
    var hrStr = hr.toString();
    if (hrStr.length == 1) hrStr = '0' + hrStr;

    // 分钟位
    var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
    var minStr = min.toString();
    if (minStr.length == 1) minStr = '0' + minStr;

    // 秒位
    var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
    var secStr = sec.toString();
    if (secStr.length == 1) secStr = '0' + secStr;
    countDownArr = {
      day: dayStr,
      hou: hrStr,
      min: minStr,
      sec: secStr
    }
    // promData[i]['countDownArr'] = countDownArr;
  }

  // this.setData({ promData: this.data.promData })
  setTimeout(this.countDown, 1000);
}
//时间戳转换
const formatNumber = n => {

  n = n.toString()

  return n[1] ? n : '0' + n

}
function formatTime(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];
  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]); 
  }
  return format;
}
module.exports = {
  countDown: countDown,
  formatTime: formatTime
}
